<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="/commons/taglib.jsp"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	<shiro:hasPermission name="MG_PERMISSION" />
	<div class="easyui-layout" style="width: fill; height: 660px; padding: 5px; margin: 5px 0px 0px 5px;">
		<div data-options="region:'west',border:false" style="width: 620px; padding: 5px;">
			<table id="mainGrid" class="easyui-datagrid" title="系统角色列表" style="width: fill; height: 650px;"
				data-options="singleSelect:true,iconCls:'icon-database',border:true,nowrap:false,rownumbers:true,url:'${patch}/system/role/listRoles.do'">
				<thead>
					<tr>
						<th data-options="field:'id',hidden:true">Id</th>
						<th data-options="field:'code',width:120,align:'center'">角色编码</th>
						<th data-options="field:'name',width:180,align:'center'">角色名称</th>
						<th data-options="field:'createDate',width:100,align:'center',formatter:dateFormat">创建日期</th>
						<th data-options="field:'description',align:'center'">描述</th>
					</tr>
				</thead>
			</table>
		</div>

		<div data-options="region:'center',border:false" style="padding: 5px;">
			<table id="itemGrid" class="easyui-datagrid" title="角色权限" style="width: fill; height: 650px;"
				data-options="singleSelect:true,iconCls:'icon-table',border:true,nowrap:false,rownumbers:true">
				<thead>
					<tr title="双击数据行，编辑显示名称.">
						<th data-options="field:'id',hidden:true">Id</th>
						<th data-options="field:'code',width:125,align:'center'">权限编码</th>
						<th data-options="field:'name',width:200,align:'center'">权限名称</th>
						<th data-options="field:'description',align:'center'">描述</th>
					</tr>
				</thead>
			</table>
		</div>
		<div id="winMain" class="easyui-window" title="角色编辑"
			data-options="modal:true,closed:true,minimizable:false,maximizable:false,collapsible:false,iconCls:'icon-save'"
			style="width: 530px; height: 380px; padding: 5px;"></div>

		<div id="winItem" class="easyui-window" title="添加角色权限"
			data-options="modal:true,closed:true,minimizable:false,maximizable:false,collapsible:false,iconCls:'icon-save'"
			style="width: 530px; height: 380px; padding: 5px;"></div>

		<script type="text/javascript">
			$('#itemGrid').datagrid(
					{
						toolbar : [
								{
									text : '添加',
									iconCls : 'icon-add',
									handler : function() {
										$('#winItem').window('open');
									}
								},
								{
									text : '删除',
									iconCls : 'icon-remove',
									handler : function() {
										var main = $("#mainGrid").datagrid('getSelected');
										var item = $("#itemGrid").datagrid('getSelected');
										if (null == main) {
											$.messager.alert("提示信息", "请选择你要调整的帐号.");
										} else if (null == item) {
											$.messager.alert("提示信息", "请选择你要删除的权限.");
										} else {
											$.messager.confirm('确认操作', '你确定要删除所选采权限吗！？', function(isOk) {
												if (isOk) {
													$.ajax({
														url : "${path}/system/role/removePermission.do?roleId=" + main.id
																+ "&permissionId=" + item.id,
														type : "GET",
														dataType : "json",
														success : function(data) {
															if (null != data.message) {
																$.messager.alert("提示信息", data.message);
															} else {
																$.messager.alert("提示信息", "删除成功！");
															}
															$('#itemGrid').datagrid('reload');
														}
													});
												}
											});
										}
									}
								} ]
					});

			$('#mainGrid').datagrid({
				toolbar : [ {
					text : '添加',
					iconCls : 'icon-add',
					handler : function() {
						$('#winMain').window({
							href : '${path}/system/role/addNew.do'
						});
						$('#winMain').window('open');
					}
				}, {
					text : '编辑',
					iconCls : 'icon-edit',
					handler : function() {
						var data = $("#mainGrid").datagrid('getSelected');
						if (null == data) {
							$.messager.alert('提示信息', '请选择要编辑的行。');
						} else {
							$('#winMain').window({
								href : '${path}/system/role/edit.do?id=' + data.id
							});
							$('#winMain').window('open');
						}
					}
				}, {
					text : '删除',
					iconCls : 'icon-remove',
					handler : function() {
						var data = $("#mainGrid").datagrid('getSelected');
						if (null == data) {
							$.messager.alert('提示信息', '请选择要删除的行。');
						} else {
							$.messager.confirm('确认操作', '你确定要删除所选帐号吗！？', function(isOk) {
								if (isOk) {
									$.ajax({
										url : '${path}/system/role/remove.do?id=' + data.id,
										type : 'GET',
										dataType : 'json',
										success : function(data) {
											if (null != data.message) {
												$.messager.alert('提示信息', data.message);
											} else {
												$.messager.alert('提示信息', '删除成功！');
											}
											refreshMainGrid();
										}
									});
								}
							});
						}
					}
				}, {
					text : '刷新',
					iconCls : 'icon-refresh',
					handler : function() {
						refreshMainGrid();
					}
				} ],

				onSelect : function(rowIndex, rowData) {
					refreshItem(rowData);
				}
			});

			$('#winMain').window({
				onClose : function() {
					refreshMainGrid();
				}
			});

			$('#winItem').window({
				onClose : function() {
					$('#itemGrid').datagrid('reload');
				}
			});

			function refreshMainGrid() {
				$('#mainGrid').datagrid('reload');
			}

			function closeWinMain() {
				$('#winMain').window('close');
			}

			function closeWinItem() {
				$('#winItem').window('close');
			}

			function refreshItem(rowData) {
				$('#itemGrid').datagrid({
					url : '${patch}/system/role/listPermissions.do?roleId=' + rowData.id
				});

				$('#winItem').window({
					href : '${path}/system/role/addPermissions.do?roleId=' + rowData.id
				});
			}
		</script>
</body>
</html>